package com.luca.framework.config;

/**
 * mybatis-plus配置
 *
 * @author luca
 */

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.luca.common.utils.SecurityUtils;
import com.luca.common.utils.StringUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MybatisPlusConfig {

    /**
     * 忽略定时任务调度表
     */
    private final static String IGNORE_PREFIX = "qrtz_";

    Map ignoreTables = new HashMap();

    public MybatisPlusConfig() {
        ignoreTables.put("sys_config", "sys_config");
        ignoreTables.put("sys_dict_type", "sys_dict_type");
        ignoreTables.put("sys_dict_data", "sys_dict_data");
        ignoreTables.put("sys_job", "sys_job");
        ignoreTables.put("sys_user_role", "sys_user_role");
        ignoreTables.put("sys_tenant", "sys_tenant");
        ignoreTables.put("sys_logininfor", "sys_logininfor");
        ignoreTables.put("sys_menu", "sys_menu");
        ignoreTables.put("sys_notice", "sys_notice");
        ignoreTables.put("sys_oper_log", "sys_oper_log");
        ignoreTables.put("gen_table", "gen_table");
        ignoreTables.put("sys_user_post", "sys_user_post");
        ignoreTables.put("sys_user_role", "sys_user_role");
        ignoreTables.put("sys_role_menu", "sys_role_menu");
        ignoreTables.put("sys_role_dept", "sys_role_dept");
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                return new LongValue(SecurityUtils.getLoginUser().getTenantId() == null ? 0 : SecurityUtils.getLoginUser().getTenantId());
            }

            // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
            @Override
            public boolean ignoreTable(String tableName) {
                if (ObjectUtils.isEmpty(SecurityUtils.getAuthentication())) {
                    return true;
                }
                if (StringUtils.equals("anonymousUser", SecurityUtils.getAuthentication().getPrincipal().toString())){
                    return true;
                }
                if (SecurityUtils.getLoginUser().getTenantId() == 0){
                    return true;
                }
                if (StringUtils.startsWith(tableName, "gen_") || StringUtils.startsWith(tableName, IGNORE_PREFIX) || StringUtils.equals("anonymousUser", SecurityUtils.getAuthentication().getPrincipal().toString())) {
                    return true;
                }
                return ignoreTables.containsKey(tableName);
            }

            @Override
            public String getTenantIdColumn() {
                return "tenant_id";
            }
        }));
        return interceptor;
    }

//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> configuration.setUseDeprecatedExecutor(false);
//    }

}

